package com.gxc.webcore.util;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ConvertUtil {

	private static SimpleDateFormat sdf = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");

	/**
	 * 将字符串转换成相应的对象类型
	 * 
	 * @param classType
	 * @param value
	 * @return
	 */
	public static Object string2Object(Class<?> clazz, String value) {
		// String
		if (clazz.equals(java.lang.String.class)) {
			return value;
		}
		// Other
		if (value == null || value.trim().length() == 0) {
			return null;
		}
		value = value.trim();
		try {
			if (clazz.equals(java.lang.Boolean.class)) {
				return Boolean.valueOf(value);
			} else if (clazz.equals(java.lang.Integer.class)) {
				return Integer.valueOf(value);
			} else if (clazz.equals(java.lang.Short.class)) {
				return Short.valueOf(value);
			} else if (clazz.equals(java.lang.Long.class)) {
				return Long.valueOf(value);
			} else if (clazz.equals(java.lang.Float.class)) {
				return Float.valueOf(value);
			} else if (clazz.equals(java.lang.Double.class)) {
				return Double.valueOf(value);
			} else if (clazz.equals(java.math.BigInteger.class)) {
				return new BigInteger(value);
			} else if (clazz.equals(java.math.BigDecimal.class)) {
				return new BigDecimal(value);
			} else if (clazz.equals(java.util.Date.class)) {
				SimpleDateFormat formatter;
				if (value.length() == 10) {
					formatter = new SimpleDateFormat("yyyy-MM-dd");
				} else if (value.length() == 16) {
					formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
				} else if (value.length() == 19) {
					formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				} else {
					formatter = new SimpleDateFormat("yyyy-MM-dd");
				}
				formatter.setLenient(false);
				return formatter.parse(value);
			} else if (clazz.equals(java.lang.Object.class)) {
				return value;
			} else {
				return null;
			}
		} catch (NumberFormatException e) {
			return null;
		} catch (ParseException e) {
			return null;
		}
	}

	/**
	 * 将对象转换成字符串
	 * 
	 * @param obj
	 * @return
	 */
	public static String object2String(Object obj) {
		if (obj == null || obj instanceof String) {
			return (String) obj;
		} else if (obj instanceof BigDecimal || obj instanceof BigInteger
				|| obj instanceof Integer || obj instanceof Float
				|| obj instanceof Boolean || obj instanceof Short
				|| obj instanceof Double || obj instanceof Long) {
			return obj.toString();
		} else if (obj instanceof Byte) {
			return StringUtil.byte2hexString((Byte) obj);
		} else if (obj instanceof Date) {
			return sdf.format(obj);
		} else if (obj instanceof java.sql.Clob) {
			try {
				return ((java.sql.Clob) obj).getSubString(1,
						(int) ((java.sql.Clob) obj).length());
			} catch (SQLException e) {
				e.printStackTrace();
				return null;
			}
		} else {
			return obj.toString();
		}
	}

	/**
	 * 将value转换成clazz类型
	 * 
	 * @param clazz
	 * @param value
	 * @return
	 */
	public static Object object2Object(Class<?> clazz, Object value) {
		if (value == null) {
			return null;
		}
		if (clazz.isInstance(value)) {
			return value;
		} else {
			return string2Object(clazz, object2String(value));
		}
	}
}
